package com.health.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.health.entity.AppointmentSetting;
import com.health.entity.pojo.dto.QueryAppointmentSettingDTO;
import com.health.entity.pojo.vo.AppointSettingVO;
import com.health.mapper.AppointmentSettingMapper;
import com.health.service.IAppointmentSettingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 预约表 服务实现类
 * </p>
 *
 * @author health
 * @since 2023-04-17
 */
@Service
public class AppointmentSettingServiceImpl extends ServiceImpl<AppointmentSettingMapper, AppointmentSetting> implements IAppointmentSettingService {
    @Override
    public List<AppointSettingVO> listAppointSetting() {
        List<AppointSettingVO> resultList = new ArrayList<>();
        List<AppointmentSetting> list = baseMapper.selectList(Wrappers.<AppointmentSetting>lambdaQuery().select());
        for (AppointmentSetting appointment : list) {
            AppointSettingVO temp = new AppointSettingVO();
            if(appointment.getStartTime() != null && timeDeal(appointment.getStartTime().substring(0,2)) <= 12){
                String timeArea = "上午" + appointment.getStartTime() + "-" + appointment.getEndTime();
                temp.setTimeArea(timeArea);
            }else if(appointment.getStartTime() != null && timeDeal(appointment.getStartTime().substring(0,2)) > 12){
                String timeArea = "下午" + appointment.getStartTime() + "-" + appointment.getEndTime();
                temp.setTimeArea(timeArea);
            }
            temp.setId(appointment.getId());
            temp.setDate(dateDeal(appointment.getDate()));
            temp.setType(appointment.getType());
            //temp.setCreateTime(appointment.getCreateTime());
            resultList.add(temp);
        }
        return resultList;
    }

    @Override
    public Integer addOrUpdateAppoint(QueryAppointmentSettingDTO appointmentDTO) {
        AppointmentSetting appointment = new AppointmentSetting();
        if(appointmentDTO.getId() != -1){
            BeanUtils.copyProperties(appointmentDTO, appointment);
            return baseMapper.updateById(appointment);
        }
        appointmentDTO.setId(null);
        BeanUtils.copyProperties(appointmentDTO, appointment);
        return baseMapper.insert(appointment);
    }


    private String dateDeal(Integer date){
        switch (date){
            case 1: return "星期一";
            case 2: return "星期二";
            case 3: return "星期三";
            case 4: return "星期四";
            case 5: return "星期五";
            case 6: return "星期六";
            case 7: return "星期天";
            default: return "法定工作日";
        }
    }

    private int timeDeal(String time){
        return time.substring(0,1).equals("0") ? Integer.parseInt(time.substring(1)) : Integer.parseInt(time);
    }
}
